/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server;

import Common.AccountInfor;
import Common.Interface;
import Common.Result;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author PhiNguyen
 */
public class Engine extends UnicastRemoteObject implements Interface {

    ArrayList<AccountInfor> lstAccInfor = new ArrayList<AccountInfor>();
    List<String> tmp = new ArrayList<String>();

    public Engine() throws RemoteException {
        loadFile();
    }
    
    public void loadFile() {
        try {
            BufferedReader br = new BufferedReader(new FileReader("account.txt"));
            String line;

            while ((line = br.readLine()) != null) {
                tmp.add(line);
            }
            int index = 0;

            for (int i = 0; i < tmp.size() / 4; i++) {
                AccountInfor accInfor = new AccountInfor();
                accInfor.setAccNumber(tmp.get(index));
                accInfor.setAccName(tmp.get(index + 1));
                accInfor.setAccPincode(tmp.get(index + 2));
                accInfor.setAccMoney(Integer.valueOf(tmp.get(index + 3)));

                lstAccInfor.add(accInfor);
                index = index + 4;
            }
        } catch (Exception ex) {
            System.out.println("Error !!!");
        }

    }
    @Override
    public Result getResult(String accNumber, String accPincode) throws RemoteException {
        Result rSult = new Result();
        for (AccountInfor acc : lstAccInfor) {
            if (accNumber.equals(acc.getAccNumber()) && accPincode.equals(acc.getAccPincode())) {
                rSult.setResult(acc.getAccName());
                break;
            } else {
                rSult.setResult("Wrong !!!");
            }
        }



        return rSult;
    }

    

    @Override
    public Result getBalance(String accNumber, String accPincode) throws RemoteException {
        Result rSultCheck = new Result();
        for (AccountInfor acc : lstAccInfor) {
            if (accNumber.equals(acc.getAccNumber()) && accPincode.equals(acc.getAccPincode())) {
                rSultCheck.setBalance("So du taikhoang cua ban: " + acc.getAccMoney());
                break;
            } else {
                rSultCheck.setBalance("Please login first!");
            }
        }
        return rSultCheck;
    }

    @Override
    public Result getChangePin(String accNumber, String accPincodeOld,String accPincode, String accPincodeConfirm) throws RemoteException {
        Result rSultChange = new Result();
        for (AccountInfor acc : lstAccInfor) {
            if (accNumber.equals(acc.getAccNumber()) && accPincodeOld.equals(acc.getAccPincode())) {
                if (accPincode.equals(accPincodeConfirm)) {
                    rSultChange.setChangePin("Ban da doi pass thanh cong");
                } else {
                    rSultChange.setChangePin("Wrong !");
                }
                break;
            }else{
                rSultChange.setChangePin("Please login first!");
            }
        }
        return rSultChange;
    }

    @Override
    public Result getWithDraw(String accNumber, int accMoney) throws RemoteException {
        Result withDraw = new Result();
        for(AccountInfor acc: lstAccInfor){
            if(accNumber.equals(acc.getAccNumber())){
                int moneyFinal = acc.getAccMoney() - Integer.valueOf(accMoney);
                withDraw.setWithDraw("So tien con lai la:" + moneyFinal);
                break;
            }else{
                withDraw.setWithDraw("Please login !");
            }
        }
        return withDraw;
    }

    @Override
    public Result getDeposit(String accNumber, int accMoney) throws RemoteException {
        Result rSultDeposit = new Result();
        for(AccountInfor acc : lstAccInfor){
            if(accNumber.equals(acc.getAccNumber())){
                int moneyFinal = acc.getAccMoney() + Integer.valueOf(accMoney);
                rSultDeposit.setDePoSit("So tien hien co trong tk la: "+moneyFinal );
                break;
            }else{
                rSultDeposit.setDePoSit("Please login !");
            }
        }
        return rSultDeposit;
    }
}
